﻿@inherits Vowei.Web.G18nWebViewPage<Vowei.Core.Models.Project>
@using Vowei.Web.Models;
@using Vowei.Core.Models;
@using Vowei.Data;
@using System.Web.Mvc.Html 
@using Vowei.Web.Helpers;

@{
    ViewBag.Title = T("编辑项目信息");    
}

<h2>@Model.Title</h2>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        
        <fieldset>
            <legend>@T("编辑项目信息")</legend>
            
            <div class="editor-label">
                @T(model => model.Title)
            </div>
            <div class="editor-field">
            <input type="hidden" id="ID" name="ID" value="@Model.ID" />
                @Html.TextBoxFor(model => model.Title)
                @Html.ValidationMessageFor(model => model.Title)
            </div>
            
            <div class="editor-label">
                @T(model => model.Owner)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Owner)
                @Html.ValidationMessageFor(model => model.Owner)
            </div>
                        
            <div class="editor-label">
                @T(model => model.StartDate)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.StartDate, WebConstants.DateTimeEditorAttributes)
                @Html.ValidationMessageFor(model => model.StartDate)
            </div>
            
            <div class="editor-label">
                @T(model => model.DueDate)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.DueDate, WebConstants.DateTimeEditorAttributes)
                @Html.ValidationMessageFor(model => model.DueDate)
            </div>
            
            <div class="editor-label">
                @T(model => model.ScmType)
            </div>
            <div class="editor-field">
                <select name="ScmType">
                    @if (Model.ScmType == Project.SupportedScm.SharePoint)
                    {
                        <option value="@Project.SupportedScm.SharePoint" selected>SharePoint</option>
                    }
                    else
                    {
                        <option value="@Project.SupportedScm.SharePoint">SharePoint</option>
                    }
                    
                    @if (Model.ScmType == Project.SupportedScm.Svn)
                    {
                        <option value="@Project.SupportedScm.Svn" selected>Svn</option>
                    }
                    else
                    {
                        <option value="@Project.SupportedScm.Svn">Svn</option>
                    }
                        
                    @if (Model.ScmType == Project.SupportedScm.Mercurial)
                    {
                        <option value="@Project.SupportedScm.Mercurial" selected>Mercurial</option>
                    }
                    else
                    {
                        <option value="@Project.SupportedScm.Mercurial">Mercurial</option>
                    }
                        
                    @if (Model.ScmType == Project.SupportedScm.Tfs)
                    {
                        <option value="@Project.SupportedScm.Tfs" selected>Tfs</option>
                    }
                    else
                    {
                        <option value="@Project.SupportedScm.Tfs">Tfs</option>
                    }
                </select>
                @Html.ValidationMessageFor(model => model.ScmType)
            </div>
            
            <div class="editor-label">
                @T(model => model.ScmUrl)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.ScmUrl)
                @Html.ValidationMessageFor(model => model.ScmUrl)
            </div>
            
			<div class="references_container">
				<h2>@T("里程碑")</h2>
				<ul class="references" id="milestones">
				   @foreach (var milestone in Model.Children)
       {
				   <li class="reference">
					  <strong>@T("名称：")</strong>(<a href="/Milestone/Details/@milestone.ID">@milestone.ID</a>): @milestone.Title
                      <input type="hidden" value="@milestone.ID" id="Children" name="Children" />
				   </li>
       }
				</ul>
				<input type="button" id="addRelatedObject" alt="/Content/tmpl/MilestoneIndex.html" value="@("添加子里程碑")" />
				<input type="button" id="createRelatedObject" alt="/Content/tmpl/MilestoneEdit.html" value="@("创建子里程碑")" />
			</div>
            <p>
                <input type="button" id="submit_btn" value="@("保存")" />
            </p>
        </fieldset>

    }

    <script type="text/javascript" language="javascript">
        $('#submit_btn').click(function () {
            var form = $('#submit_btn').parents('form');
            var arr = $(form).serializeArray();

            var project = {};
            var timeline = [];
            $(arr).each(function (i, a) {
                if (a.name != 'Children')
                    project[a.name] = a.value;
                else
                    timeline.push({ ID: a.value });
            });

            project.Children = timeline;

            $.ajax({
                type: 'POST',
                url: $(form).attr('action'),
                dataType: 'json',
                data: JSON.stringify(project),
                contentType: 'application/json; charset=utf-8',
                success: function () {
                    window.location = "/Project/Details/@Model.ID";
                }
            });
        });
    </script>

    <div>
        @Html.ActionLink(T("返回项目列表"), "Index")
    </div>

	<script type="text/html" id="milestone_tmpl">	
		<li class="reference">
			<strong>名称：</strong>(<a href="/Milestone/Details/${ID}">${ID}</a>): ${Title}
		</li>
        <input type="hidden" value="${ID}" id="Children" name="Children" />
	</script>
    <div id="child_editor" class="subform"></div>
	<script language="javascript" type="text/javascript">
        function applyTemplate(templateText, data, okfunc) {
            var editor = $('#child_editor');
            var elem = $.tmpl(templateText, data);
            elem.appendTo(editor);
            $('input[type="submit"]', elem).remove();
            fix_input_boxes();

            $(editor).dialog({
                modal: true,
                width: 450,
                buttons: {
                    Ok: okfunc
                }
            });

	        return editor;
        };

        function selectingChildItem(data) {
	        $('#child_editor').empty();
	        var templateText = data;
	        $.ajax({
	            url: '/Milestone/UnAssigned',
	            dataType: 'json',
	            success: function (data) {
	                var wrapper = {
	                    dataContext: eval(data)
	                };

	                var dialog = applyTemplate(templateText, wrapper, function () {
                        var elems = $(this).find('#select_item_chk');
                        $.each(elems, function (i, a) {
                            if ($(a).attr('checked')) {
                                var item = wrapper.dataContext[i];
                                $('#milestone_tmpl').tmpl(item).appendTo('#milestones');
                            }
                        });

                        $(this).dialog('close');
                    });
	            }
	        });
        };

        function creatingMilestone(data) {
            $('#child_editor').empty();
            var templateText = data;
            var dataContext = {
                Title: null,
                DueDate: null,
                StartDate: null
            };

            var dialog = applyTemplate(templateText, dataContext, function () {
                var arr = $('form', this).serializeArray();
                var postData = {}
                $(arr).each(function (i, a) {
                    postData[a.name] = a.value;
                });

                $.ajax({
                    url: '/Milestone/Create',
                    type: 'POST',
                    dataType: 'json',
                    data: postData,
                    success: function (data) {
                        $('#milestone_tmpl').tmpl(data).appendTo('#milestones');
                    }
                });

                $(this).dialog('close');
            });
        };

	    function openModelWindow(address, callback) {
            $.ajax({
                url: address,
                success: callback
            });
	    };

	    function registerHandler(selector, callback) {
	        $(selector).click(function () {
	            var url = $(this).attr('alt');
	            openModelWindow(url, callback);
	        });            
	    };

	    registerHandler('#addRelatedObject', selectingChildItem);
	    registerHandler('#createRelatedObject', creatingMilestone);
	</script>

